package org.shipp.util;

import java.util.LinkedList;

import org.shipp.activity.PrincipalActivity;
import org.shipp.activity.R;
import org.shipp.model.Item;

import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
/**
 * 
 * @author Efren
 *
 */
public class MenuEventController {
    private static Display display;
    
    public static void open(final Context context, final RelativeLayout layout, final RelativeLayout topo, final float ondeEsta, long tempo){
    	display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    	if(tempo > PrincipalActivity.TEMPO_PADRAO){
    		tempo = PrincipalActivity.TEMPO_PADRAO;
    	}

    	float left = (float) (display.getWidth() * PrincipalActivity.INDICE_PORCENTAGEM_TELA_PADRAO);
    	TranslateAnimation ta = new TranslateAnimation(ondeEsta, left, 0f, 0f);
    	ta.setDuration(tempo);
    	ta.setFillEnabled(true);
    	ta.setFillAfter(true);
    	
    	layout.startAnimation(ta);
    	ta.setAnimationListener(new Animation.AnimationListener(){
    	    @Override
    	    public void onAnimationStart(Animation arg0) {
    	    }           
    	    @Override 
    	    public void onAnimationRepeat(Animation arg0) {
    	    }           
    	    @Override
    	    public void onAnimationEnd(Animation arg0) {
    	    	TranslateAnimation anim = new TranslateAnimation(0f, 0f, 0.0f, 0.0f);
                anim.setDuration(1);

                layout.startAnimation(anim);
                
                int left = (int) (display.getWidth() * PrincipalActivity.INDICE_PORCENTAGEM_TELA_PADRAO);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(layout.getLayoutParams());
                params.setMargins(left, 0, 0, 0);
                params.width = display.getWidth();
                layout.setLayoutParams(params);
                
            	int left2 = (int) (display.getWidth() * PrincipalActivity.INDICE_PORCENTAGEM_TELA_PADRAO);
            	
            	for(int i=0; i<((ViewGroup)layout).getChildCount(); ++i) {
            		View nextChild = ((ViewGroup)layout).getChildAt(i);
            		if(nextChild.getId() == R.id.web){
            			RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(nextChild.getLayoutParams());
            			params2.setMargins(0, 0, (left2 * -1), 0);
            			params2.addRule(RelativeLayout.BELOW, R.id.actionBar);
            			nextChild.setLayoutParams(params2);
            			
            			
            			nextChild.setOnTouchListener(new OnSwipeTouchListener(context, layout) {
            				
            				public void onSwipeRight(float ondeEsta, long tempo) {
            					if (!PrincipalActivity.open) {
            						PrincipalActivity.open = true;
            						MenuEventController.open(context, layout, topo, ondeEsta, tempo);
            						MenuEventController.closeKeyboard(context, layout);
            					}
            				}
            				
            				public void onSwipeLeft(float ondeEsta, long tempo) {
            					if (PrincipalActivity.open) {
            						PrincipalActivity.open = false;
            						MenuEventController.close(context, layout, topo, ondeEsta, tempo);
            						MenuEventController.closeKeyboard(context, layout);
            					}
            				}
            			});

            		}

            	}
            	for(int i=0; i<((ViewGroup)topo).getChildCount(); ++i) {
            		View nextChild = ((ViewGroup)topo).getChildAt(i);
            		if(nextChild.getId() == R.id.ib_logo){
            			RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(nextChild.getLayoutParams());
            			int tamLEFT = (topo.getWidth() / 2) - (nextChild.getWidth() / 2);
            			int tamTOP = (topo.getHeight() / 2) - (nextChild.getHeight() / 2);
            			int tamRIGHT = left2 * -1;
            			params2.setMargins(tamLEFT, tamTOP, tamRIGHT, 0);
            			nextChild.setLayoutParams(params2);
            		}
            	}
    	    }
    	});
    }
    
    public static void close(final Context context, final RelativeLayout layout, RelativeLayout topo, final float ondeEsta, long tempo){
    	if(tempo > PrincipalActivity.TEMPO_PADRAO){
    		tempo = PrincipalActivity.TEMPO_PADRAO;
    	}
    	
    	TranslateAnimation ta = new TranslateAnimation(ondeEsta, 0f, 0f, 0f);
    	
    	ta.setDuration(tempo);
    	layout.startAnimation(ta);

    	RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
    	params.setMargins(PrincipalActivity.RECUO_PADRAO, 0, 0, 0);
    	params.width = display.getWidth() + 3;
    	layout.setLayoutParams(params);
    	
    	for(int i=0; i<((ViewGroup)layout).getChildCount(); ++i) {
    		View nextChild = ((ViewGroup)layout).getChildAt(i);
    		if(nextChild.getId() == R.id.web){
    			RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(nextChild.getLayoutParams());
    			params2.addRule(RelativeLayout.BELOW, R.id.actionBar);
    			params2.setMargins(0, 0, 0, 0);
    			nextChild.setLayoutParams(params2);
    			nextChild.setOnTouchListener(null);
    		}
    	}
    	
    	for(int i=0; i<((ViewGroup)topo).getChildCount(); ++i) {
    		View nextChild = ((ViewGroup)topo).getChildAt(i);
    		if(nextChild.getId() == R.id.ib_logo){
    			RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(nextChild.getLayoutParams());
    			params2.setMargins(0, 0, 0, 0);
    			params2.addRule(RelativeLayout.CENTER_HORIZONTAL);
    			params2.addRule(RelativeLayout.CENTER_VERTICAL);
    			nextChild.setLayoutParams(params2);
    		}
    	}



    }
    
    public static void closeKeyboard(final Context context, final View view){
    	InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    
    public static LinkedList<Item> getMenuDefault(final Context context){
		LinkedList<Item> listItemMenu = MenuConfigAdapter.getMenuDefault();
		return listItemMenu;
    }

}
